package leetcode.editor.cn.dsa01_array;
//编写一个函数，输入是一个无符号整数（以二进制串的形式），返回其二进制表达式中数字位数为 '1' 的个数（也被称为汉明重量）。
public class NumberOf1Bits191_1 {
    public static void main(String[] args) {
        NumberOf1Bits191_1.Solution solution = new NumberOf1Bits191_1().new Solution();
        System.out.println(solution.hammingWeight(0b1011)); //3
        System.out.println(solution.hammingWeight(0b01000000)); //1
        System.out.println(solution.hammingWeight(0b11111111111111111111111111111101)); //31
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    public class Solution {
        // you need to treat n as an unsigned value
        public int hammingWeight(int n) {
            int count = 0;
            // 不断让n去 & n-1，直到 & 到0为止
            while (n != 0) {
                count++;
                n&=(n-1);
            }
            return count;
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)
}